<?php

Loader::using("models::entities::enums::TipoReporte");
Loader::using("models::entities::enums::EstadoReporte");


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of ReportarAmonestacion
 *
 * @author EArzuza
 * export-metadata RequiredRoles:ROLE_ENTERPRISE
 *
 * @export Modules
 */
class ReportarAmonestacion extends Module {

    public function index() {
        
        $nit= $this->request->get("NitEmpresa", NULL);
        $TiposReportes = TipoReporte::values();
        $todasSolicitudes = array();
        $Solicitudes = array();
        $Practicantes = array();
        $todasSolicitudes = $this->model->applications->GetByEmpresa($nit);
        
        foreach ($todasSolicitudes as $Solicitud) {
            if($Solicitud->getEstado() == EstadoSolicitud::Procesada ){
                 array_push($Solicitudes, $Solicitud);
            }           
        }
     
        $Empresa= $this->model->companies->GetById($nit);
        foreach ($Solicitudes as $Solicitud) {
            if($Solicitud->getPracticanteActual()!=null){
                 array_push($Practicantes, $Solicitud->getPracticanteActual());
            }           
        }
       
        $this->assign("Practicantes", $Practicantes);
        $this->assign("action", $this->route->action("Guardar"));
        $this->assign("TiposReportes", $TiposReportes);
        $this->assign("Empresa", $Empresa);
            
        $this->display();
    
    }
    

    public function Guardar() {
        
        Loader::using("models::entities::Reportes");
        
        
        if ($this->request->submitted()) {
         
            $reporte = new Reportes();
                      
            //$this->request->bind($reporte);
            $reporte->setDescripcion($this->request->post("DescripcionReporte"));
            $reporte->setFecha(MicrosoftDateFormatter::toJSON(date("d/m/Y")));
            $tipoReporte = $this->request->post("TipoReporte");
            
            $codigoEstudiante =   $this->request->post("CodigoEstudiante");
            
             if($tipoReporte==TipoReporte::Retiro){
                 $practicante = $this->model->practitioners->GetByEstudiante($codigoEstudiante);
                 $solicitud = $this->model->applications->GetById($practicante->getIdSolicitud());
                 $solicitud->setEstado(EstadoSolicitud::Cancelada);
                 $this->model->applications->Update($solicitud);
             }
                
            $reporte->setTipoReporte($tipoReporte);
            $reporte->setEstadoReporte(EstadoReporte::Enviado);
            $reporte->setIdPracticante($this->request->post("idPracticante"));
            $this->model->reports->Create($reporte);
            $this->addMessage(new Message("", "Reporte Creado Exitosamente !!!", Message::SUCCESS));
            $this->assign("UrlVolver", $this->route->moduleAction("GestionEmpresas", "index"));
            $this->display();
           
        } else {
           
            $this->index();
        }
    }
    
    public function Actualizar() {
        
        $Reporte = new Reportes();
        if ($this->request->submitted()){
            $Reporte = $this->request->bind($Reporte);
            $this->model->reports->update(Reporte);
            
            $this->display();
                   
        }else{
            $this->index();
        }
        
    }

}

?>
